package com.cjkj.equipment.controller;

import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.utils.DateUtils;
import com.cjkj.common.core.utils.bean.BeanUtils;
import com.cjkj.equipment.domain.TWarnRecords;
import com.cjkj.equipment.service.ITWarnRecordsService;
import com.cjkj.system.api.domain.APIWarnHandle;
import com.cjkj.system.api.domain.APIWarnRecord;
import com.cjkj.system.api.domain.APIWarnRecords;
import com.cjkj.system.api.domain.request.WarnListReq;
import com.cjkj.system.api.domain.request.WarnRecordReq;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 预警接口类
 */
@RestController
@RequestMapping("/warn")
public class EquipmentWarnController {

    @Resource
    private ITWarnRecordsService recordsService;

    @PostMapping("/list")
    R<APIWarnRecords> listWarn(@RequestBody WarnListReq req){
        List<TWarnRecords> recordsList = recordsService.listRecords(req.getHospitalId(), req.getDeptId(), req.getHasHandle(), req.getDataType());
        APIWarnRecords apiWarnRecords = new APIWarnRecords();
        List<APIWarnRecord> apiWarnRecordList = new ArrayList<>();
        recordsList.forEach(e->{
            APIWarnRecord apiWarnRecord = new APIWarnRecord();
            BeanUtils.copyProperties(e,apiWarnRecord);
            apiWarnRecordList.add(apiWarnRecord);
        });
        apiWarnRecords.setWarnRecordList(apiWarnRecordList);
        return R.ok(apiWarnRecords);
    }

    @PostMapping("/handle")
    R<Boolean> handleWarns(@RequestBody List<APIWarnHandle> apiWarnHandleList){
        return R.ok(recordsService.handleWarn(apiWarnHandleList));
    }

    @PostMapping("/handle/visitNo")
    R<Boolean> handleWarns(@RequestBody WarnRecordReq req){
        return R.ok(recordsService.handleWarn(req));
    }

    @GetMapping("/list/{visitNo}")
    R<APIWarnRecords> list(@PathVariable("visitNo")Long visitNo, String expectOutTime){
        List<TWarnRecords> warnRecordsList = recordsService.list(visitNo, DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, expectOutTime));
        List<APIWarnRecord> apiWarnRecordList = new ArrayList<>();
        warnRecordsList.forEach(e->{
            APIWarnRecord apiWarnRecord = new APIWarnRecord();
            BeanUtils.copyProperties(e,apiWarnRecord);
            apiWarnRecordList.add(apiWarnRecord);
        });
        APIWarnRecords apiWarnRecords = new APIWarnRecords();
        apiWarnRecords.setWarnRecordList(apiWarnRecordList);
        return R.ok(apiWarnRecords);
    }
}
